/*
 * SonarLint for IntelliJ IDEA
 * Copyright (C) 2015-2024 SonarSource
 * sonarlint@sonarsource.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonarlint.intellij.finding.issue.vulnerabilities

import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import org.sonarlint.intellij.finding.Flow
import org.sonarlint.intellij.finding.Location
import org.sonarlint.intellij.finding.TextRangeMatcher
import org.sonarlint.intellij.finding.TextRangeMatcher.NoMatchException
import org.sonarlint.intellij.finding.fileOnlyLocation
import org.sonarlint.intellij.finding.resolvedLocation
import org.sonarlint.intellij.finding.unknownLocation
import org.sonarlint.intellij.util.ProjectUtils.tryFindFile
import org.sonarlint.intellij.util.SonarLintAppUtils.findModuleForFile
import org.sonarsource.sonarlint.core.rpc.protocol.backend.tracking.TaintVulnerabilityDto
import org.sonarsource.sonarlint.core.rpc.protocol.backend.tracking.TaintVulnerabilityDto.FlowDto.LocationDto
import org.sonarsource.sonarlint.core.rpc.protocol.backend.tracking.TextRangeWithHashDto
import java.nio.file.Path

class TaintVulnerabilityMatcher(private val project: Project) {
  private val textRangeMatcher = TextRangeMatcher(project)

  fun match(remoteIssue: TaintVulnerabilityDto): LocalTaintVulnerability {
      val primaryLocation = matchLocation(remoteIssue)

      return LocalTaintVulnerability(
        primaryLocation.file?.let { findModuleForFile(it, project) },
        primaryLocation,
        remoteIssue.flows.mapIndexed { index, f -> Flow(index + 1, f.locations.reversed().map { matchLocation(it) }) },
        remoteIssue,
        remoteIssue.isResolved)
  }

  private fun matchLocation(taintIssue: TaintVulnerabilityDto): Location {
    return matchLocation(taintIssue.ideFilePath!!, taintIssue.textRange, taintIssue.message)
  }

  private fun matchLocation(location: LocationDto): Location {
    return matchLocation(location.filePath!!, location.textRange, location.message)
  }

  private fun matchLocation(filePath: Path, textRange: TextRangeWithHashDto?, message: String): Location {
    val matchedFile = tryFindFile(project, filePath)

    return if (matchedFile != null) matchTextRange(matchedFile, textRange, message) else unknownLocation(message, filePath)
  }

  private fun matchTextRange(matchedFile: VirtualFile, textRange : TextRangeWithHashDto?, message: String): Location {
    if (textRange == null) {
      return fileOnlyLocation(matchedFile, message)
    }
    return try {
      val rangeMarker = textRangeMatcher.match(matchedFile, textRange)
      resolvedLocation(matchedFile, rangeMarker, message, textRange.hash)
    } catch (e: NoMatchException) {
      fileOnlyLocation(matchedFile, message)
    }
  }
}
